package petstore.persistence;

import petstore.domain.Location;
import petstore.persistence.impl.LocationDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class LocationDaoImpl implements LocationDao
{
    private final int uid;
    private static final String GET_LOCATIONS = "SELECT user_id,location_id,region,detail FROM location WHERE user_id = ";
    private static final String ADD_LOCATION = "INSERT INTO location (user_id, region, detail)VALUES(?,?,?)";
    private static final String REMOVE_LOCATION = "DELETE FROM location WHERE user_id = ? AND location_id = ?";
    private static final String GET_LID = "SELECT location_id FROM location WHERE  user_id = ? AND region = ? AND detail = ?";
    private static final String EDIT_LOCATION = "UPDATE location SET region = ? , detail = ? WHERE location_id = ?";
    private List<Location> locationList = new ArrayList<>();

    public LocationDaoImpl(int uid)
    {
        this.uid = uid;
    }

    @Override
    public List<Location> getLocation()
    {
        try
        {
            Connection connection = DBUtil.getConnection();
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(GET_LOCATIONS + uid);
            while (resultSet.next())
            {
                Location location = new Location();
                location.setUid(resultSet.getInt("user_id"));
                location.setLocationId(resultSet.getInt("location_id"));
                location.setRegion(resultSet.getString("region"));
                location.setDetail(resultSet.getString("detail"));
                locationList.add(location);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        System.out.println("locationList:");
        for(Location location:locationList)
        {
            System.out.println("id: "+location.getLocationId()+" location:"+location.getRegion()+location.getDetail());
        }
        return locationList;
    }


    @Override
    public int addLocation(String region, String detail)
    {
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(ADD_LOCATION);
            System.out.println(uid+region+detail);
            preparedStatement.setString(1,""+uid);
            preparedStatement.setString(2,region);
            preparedStatement.setString(3,detail);
            int rows = preparedStatement.executeUpdate();
            if(rows == 1)
            {
                System.out.println("添加成功");
                PreparedStatement preparedStatement1 = connection.prepareStatement(GET_LID);
                preparedStatement1.setString(1,""+uid);
                preparedStatement1.setString(2,region);
                preparedStatement1.setString(3,detail);
                ResultSet resultSet = preparedStatement1.executeQuery();
                resultSet.next();
                return resultSet.getInt("location_id");
            }
            else
            {
                System.out.println("添加失败");
                return -1;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public boolean removeLocation(int locationId)
    {
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(REMOVE_LOCATION);
            preparedStatement.setString(1,""+uid);
            preparedStatement.setString(2,""+locationId);
            int rows = preparedStatement.executeUpdate();
            if(rows == 1)
            {
                System.out.println("删除成功");
                return true;
            }
            else
            {
                System.out.println("删除失败");
                return false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean editLocation(int locationId, String region, String detail)
    {
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(EDIT_LOCATION);
            preparedStatement.setString(1,region);
            preparedStatement.setString(2,detail);
            preparedStatement.setString(3,""+locationId);
            int row = preparedStatement.executeUpdate();
            if(row == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
}
